package com.example.leo.mycharmingstudent.a0917;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0916.HeroEntity;

import java.util.List;

/**
 * Created by Leo on 2020/9/17.
 */

public class MultiViewTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private List<HeroEntity> mHeroList;
    private Context mContext;

    public MultiViewTypeAdapter(List<HeroEntity> heroList, Context context) {
        this.mHeroList = heroList;
        this.mContext = context;
    }

    /**
     * 返回值就是onCreateViewHolder方法的第二个参数
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return mHeroList.get(position).getType();
    }

    /**
     * 创建ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case 0:
                //创建详情ViewHolder
                View inflate = LayoutInflater.from(mContext)
                        .inflate(R.layout.item_cardview_listview_activity, parent, false);
                return new DetailViewHolder(inflate);
            case 1:
                //创建图片ViewHolder
                View imageItemView = LayoutInflater.from(mContext)
                        .inflate(R.layout.item_cardview_listview_activity2, parent, false);
                return new ImageViewHolder(imageItemView);
        }
        return null;
    }

    /**
     * 设置数据
     * 给ViewHolder中的控件，设置数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HeroEntity entity = mHeroList.get(position);

        if (holder instanceof DetailViewHolder) {
            DetailViewHolder detailViewHolder = (DetailViewHolder) holder;
            Glide.with(mContext)
                    .load(entity.getImageResource())
                    .into(detailViewHolder.ivHero);
            detailViewHolder.tvName.setText(entity.getName());
            detailViewHolder.tvIntroduce.setText(entity.getIntroduce());
        } else if (holder instanceof ImageViewHolder) {
            ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
            Glide.with(mContext)
                    .load(entity.getImageResource())
                    .into(imageViewHolder.ivHero);
        } else {
        }


    }

    /**
     * 条目数量
     * @return
     */
    @Override
    public int getItemCount() {
        return mHeroList.size();
    }

    public static class DetailViewHolder extends RecyclerView.ViewHolder{

        private ImageView ivHero;
        private TextView tvName;
        private TextView tvIntroduce;

        public DetailViewHolder(View itemView) {
            super(itemView);
            ivHero = itemView.findViewById(R.id.iv_hero_item_cardview);
            tvName = itemView.findViewById(R.id.tv_hero_name_item_cardview);
            tvIntroduce = itemView.findViewById(R.id.tv_hero_introduce_item_cardview);
        }
    }

    public static class ImageViewHolder extends RecyclerView.ViewHolder{
        private ImageView ivHero;
        public ImageViewHolder(View itemView) {
            super(itemView);
            ivHero = itemView.findViewById(R.id.iv_hero_item_cardview);
        }
    }
}
